package ch.zbw.pra.sysventory.ui.gui.panels.overviewandsearchpanel.subpanels;

import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;

import ch.zbw.pra.sysventory.domain.model.Computer;
import ch.zbw.pra.sysventory.domain.visualisation.VisuControll;
import ch.zbw.pra.sysventory.ui.gui.panels.overviewandsearchpanel.ComputerTableModel;
import ch.zbw.pra.sysventory.ui.gui.panels.overviewandsearchpanel.subpanels.detailPanel.ComputerDetailViewDialog;

public class SearchResultPanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2577884064104410758L;
	private JTable searchResultTable;
	private ComputerTableModel model;
	private JScrollPane scrollPane;
	private List<Computer> computerList;

	public SearchResultPanel() {
		super(new BorderLayout());
		this.computerList = new ArrayList<Computer>();
		this.searchResultTable = new JTable();
		this.searchResultTable.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount() == 2) {
					int selectedRowIndex = SearchResultPanel.this.searchResultTable.getSelectedRow();
					if (selectedRowIndex >= 0 && selectedRowIndex < SearchResultPanel.this.computerList.size()) {
						Computer computer = SearchResultPanel.this.computerList.get(selectedRowIndex);
						new ComputerDetailViewDialog(computer);
					}
				}
			}
		});

		this.searchResultTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		this.searchResultTable.setRowHeight(25);
		this.model = new ComputerTableModel();
		this.searchResultTable.setModel(this.model);
		this.searchResultTable.setDefaultRenderer(new ArrayList<String>().getClass(), this.model);
		this.searchResultTable.setAutoCreateRowSorter(true);
		this.scrollPane = new JScrollPane(this.searchResultTable);
		this.scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		this.scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
		this.add(this.scrollPane, BorderLayout.CENTER);
	}

	public void setComputerList(List<Computer> list) {
		if (list != null) {
			this.computerList = list;

			VisuControll visucontroll = VisuControll.getInstance();
			List<String> header = new ArrayList<String>();
			visucontroll.addDataToColumn(header);
			List<List<List<String>>> data = new ArrayList<List<List<String>>>();
			visucontroll.addDataToRow(this.computerList, data);
			this.model = new ComputerTableModel(data, header);
			this.searchResultTable.setModel(this.model);
			this.searchResultTable.setDefaultRenderer(new ArrayList<String>().getClass(), this.model);
			this.searchResultTable.setDefaultEditor(new ArrayList<String>().getClass(), this.model);
			this.searchResultTable.setBackground(new JLabel().getBackground());
			setColumnWidthOfTable();
		}
	}

	private void setColumnWidthOfTable() {
		TableColumnModel model = this.searchResultTable.getColumnModel();
		for (int i = 0; i < this.searchResultTable.getModel().getColumnCount(); i++) {
			TableColumn column = model.getColumn(i);
			column.setMinWidth(100);
			column.setPreferredWidth(150);
			column.setMaxWidth(300);
		}
	}
}
